/*********** CLIENT TCP **************/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <string.h> 
#include <memory.h>
#define SERVEUR "127.0.0.1" 
#define PORTS "2058" 

main() { 
    int sockfd, new_fd, rv, sin_size;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr their_addr;
    char buf[100];
    char s[256];
    int numbytes;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM;
    rv = getaddrinfo(SERVEUR, PORTS, &hints, &servinfo);
    if (rv != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    } 
    
    // Création socket et attachement
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) 
        {
        perror(" client: socket");
        continue;
        }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfd);
        perror("client: connect");
        continue;
    }
    break;
    }
    
    if (p == NULL) {
        fprintf(stderr, "server: failed to bind\n");
        return 2;
    }
    freeaddrinfo(servinfo); // Libère structure
    if ((numbytes = recv(sockfd, buf, 100-1, 0)) == -1)
    { 
        perror("recv");
        exit(1);
    }
printf("Message reçu : %s\n",buf);

printf("Entrez un message \n");
scanf("%s",s);
printf("%s \n",s);
fgets(s,255,stdin);
send(sockfd, s, strlen(s),0);//à revoir
close(sockfd);
return 0;
}
